<?php


namespace app\api\controller;


use app\BaseController;
use app\middleware\Auth;
use think\middleware\AllowCrossDomain;


class Safe extends BaseController
{
    protected $middleware = [AllowCrossDomain::class,Auth::class];

    public function index()
    {

    }

    public function change_password()
    {
        $userid = $this->request->userid;
        $old_password = $this->request->post('old_password', '');
        $new_password = $this->request->post('new_password', '');
        $new_password2 = $this->request->post('new_password2', '');
        if (!$old_password) {
            return $this->msg(0, '旧密码不能为空');
        }
        if (!$new_password) {
            return $this->msg(0, '新密码不能为空');
        }
        if (!$new_password2) {
            return $this->msg(0, '再次新密码不能为空');
        }
        if ($new_password != $new_password2) {
            return $this->msg(0, '两次新密码不一致');
        }
        $user = \think\facade\Db::name('member')->where(['userid' => $userid])->find();
        if (md5($old_password) != $user['password']) {
            return $this->msg(0, '旧密码不正确');
        }

        $r = \think\facade\Db::name('member')->where(['userid' => $userid])->update(['password' => md5($new_password)]);
        return $this->msg($r === false ? false : true);
    }
}